/**
 * 
 */
package org.vsg.stock.basic.service.volfigure;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.vsg.stock.basic.domain.VolFigureDetails;
import org.vsg.stock.basic.domain.VolFigureKlineDaily;
import org.vsg.stock.basic.domain.VolFigureKlineDailyPK;
import org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor;
import org.vsg.stock.core.domain.StockDailyKLine;
import org.vsg.stock.core.indicator.kline.ZigIndicatorResultItem;

/**
 *  处理平量柱模式1，凹口平量柱，一左一右两根高度持平的量柱内部为3-4根
 * @author Bill Vison
 *
 */
public class UnbiasedMod2Processor implements VolFigureKlineDailyProcessor {

	static private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	
	
	private Set<String> dailyKeys = new HashSet<String>();	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#processDailyData(java.util.List)
	 */
	@Override
	public List<VolFigureKlineDaily> processDailyData(
			List<StockDailyKLine> stkDailyKLineList) {

		List<VolFigureKlineDaily> klineDailyList = new Vector<VolFigureKlineDaily>();		
		
		double rate = 0.95;

		StockDailyKLine startLeveKline = null;
		for (int i = 0 ; i < stkDailyKLineList.size() ; i++) {
			
			boolean preCheckResult = preCheckFit(stkDailyKLineList , i );

			if (!preCheckResult) {
				continue;
			}
			
			startLeveKline = stkDailyKLineList.get(i);
			// ---- find the new vol ---
			int passIndex = handleInvoke(stkDailyKLineList , i);
			
		
			if ( passIndex > 0) {

				String indexLine = startLeveKline.getId().getStockCode()+","+detail.getCode()+","+df.format( startLeveKline.getDate() );
				
				if (!dailyKeys.contains( indexLine) ) {
					Date markEndDate = stkDailyKLineList.get( i+passIndex ).getDate();
					
					// --- mark the date ---
					VolFigureKlineDailyPK idPk = new VolFigureKlineDailyPK();
					idPk.setCodeVolFigureDetails( this.detail.getCode() );
					idPk.setStockCode( startLeveKline.getId().getStockCode() );
					idPk.setStockStartDate( startLeveKline.getId().getDate() );

					// --- gen new data object ---
					VolFigureKlineDaily dailyInst = new VolFigureKlineDaily();
					dailyInst.setId( idPk );
					dailyInst.setStockDateEnd( markEndDate );
					
					klineDailyList.add( dailyInst );
				}
				
				i += passIndex; 
				
			}		

					
		}
		

		return klineDailyList;
	}
	
	// --- check the list is fit to char processor
	private boolean preCheckFit(List<StockDailyKLine> stkDailyKLineList , int index) {
		
		
		StockDailyKLine levelKLine = stkDailyKLineList.get( index );
		long levelKLineVol = levelKLine.getVol();

		for (int i = 0 ; i < 10 ; i++) {
			if ( index + i >= stkDailyKLineList.size() ) {
				return false;
			}
			// --- no need the first value
			if ( i == 0 ) {
				continue;
			}

			int currentIndex = index + i;			

			double compareValue =  (double)stkDailyKLineList.get(currentIndex).getVol() - levelKLineVol;
			double compareRate = compareValue / levelKLineVol;
			
			// --- 太少柱，且出现>比较柱 ，不能比较，则为 false
			if ( i < 4 && compareRate > 0.05) {
				return false;
			}
			
			if ( Math.abs( compareRate ) <= 0.065) {

				return true;
			}
			
		}
		
		return false;
	}
	
	/**
	 * this method is not implement
	 * @param stkDailyKLineList
	 * @param index
	 * @return
	 */
	private int handleInvoke(List<StockDailyKLine> stkDailyKLineList , int index) {

		double rate = 0.95;		
		


		// --- not include the current date
		return -1;
	}
	
	
	private List<ZigIndicatorResultItem> crests;

	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setCrestsInCycle(java.util.List)
	 */
	@Override
	public void setCrestsInCycle(List<ZigIndicatorResultItem> crests) {
		this.crests = crests;

	}

	@Override
	public void setExistedVolFigureKineDaily(
			List<VolFigureKlineDaily> volFigureKlineDailyList) {
		// --- get the daily ---
		VolFigureKlineDailyPK id = null;
		for (VolFigureKlineDaily daily : volFigureKlineDailyList) {
			id = daily.getId();
			String keyLine = id.getStockCode()+ "," + id.getCodeVolFigureDetails()+"," + df.format( id.getStockStartDate() );
			dailyKeys.add( keyLine );
		}
		
	}

	private VolFigureDetails detail;	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setCurrentVolFigureDetails(org.vsg.stock.basic.domain.VolFigureDetails)
	 */
	@Override
	public void setCurrentVolFigureDetails(VolFigureDetails detail) {
		this.detail = detail;

	}

	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setTroughsInCycle(java.util.List)
	 */
	@Override
	public void setTroughsInCycle(List<ZigIndicatorResultItem> troughs) {
		// TODO Auto-generated method stub

	}

}
